﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NetFrameworkExtensions.Utils
{
  [Serializable]
  public class BackupFieldReference<T> : ReferenceBase<T>
  {
    T value;
    bool canChange;

    public BackupFieldReference(T value, bool canChange)
    {
      this.value = value;
      this.canChange = canChange;
    }
    public BackupFieldReference(T value) : this(value, true)
    {
    }
    public BackupFieldReference() : this(default(T), true)
    {
    }

    public override T Value
    {
      get
      {
        return value;
      }
      set
      {
        CheckCanChange();
        T previousValue = value;
        this.value = value;
        OnValueChanged(value, previousValue);
      }
    }

    public override bool CanChange
    {
      get { return canChange; }
    }
  }
}
